package info.cs52.fes.action.auth;

import info.cs52.fes.FesAction;
import info.cs52.fes.action.SessionUtils;
import info.cs52.fes.dao.UserDAO;
import info.cs52.fes.data.User;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

@SuppressWarnings("serial")
public class LoginAction extends FesAction {
		
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		UserService userService = UserServiceFactory.getUserService();
		com.google.appengine.api.users.User gUser = userService.getCurrentUser();
		
        if (gUser == null) {
            resp.sendRedirect(userService.createLoginURL(req.getRequestURI()));
        } else {
        	User user = UserDAO.findByServiceUserId(gUser.getUserId());
        	if (user == null) {
				resp.sendRedirect("/auth/register");
			} else {
				SessionUtils.login(req, user);
				resp.sendRedirect("/dial");
			}
        }
	}
}
